home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / alpha.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  4KB  |  138 lines

  1. /* $Id: alpha.c,v 3.3 1998/07/29 04:02:30 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: alpha.c,v $
  26.  * Revision 3.3  1998/07/29 04:02:30  brianp
  27.  * fixed rounding error in computing integer alpha reference value
  28.  *
  29.  * Revision 3.2  1998/03/28 03:57:13  brianp
  30.  * added CONST macro to fix IRIX compilation problems
  31.  *
  32.  * Revision 3.1  1998/02/13 03:23:04  brianp
  33.  * AlphaRef is now a GLubyte
  34.  *
  35.  * Revision 3.0  1998/01/31 20:44:19  brianp
  36.  * initial rev
  37.  *
  38.  */
  39.  
  40.  
  41. #ifdef PC_HEADER
  42. #include "all.h"
  43. #else
  44. #include "alpha.h"
  45. #include "context.h"
  46. #include "types.h"
  47. #include "macros.h"
  48. #endif
  49.  
  50.  
  51.  
  52. void gl_AlphaFunc( GLcontext* ctx, GLenum func, GLclampf ref )
  53. {
  54.    if (INSIDE_BEGIN_END(ctx)) {
  55.       gl_error( ctx, GL_INVALID_OPERATION, "glAlphaFunc" );
  56.       return;
  57.    }
  58.    switch (func) {
  59.       case GL_NEVER:
  60.       case GL_LESS:
  61.       case GL_EQUAL:
  62.       case GL_LEQUAL:
  63.       case GL_GREATER:
  64.       case GL_NOTEQUAL:
  65.       case GL_GEQUAL:
  66.       case GL_ALWAYS:
  67.          ctx->Color.AlphaFunc = func;
  68.          ctx->Color.AlphaRef = (GLubyte) (CLAMP(ref, 0.0F, 1.0F) * 255.0F + 0.5F);
  69.          break;
  70.       default:
  71.          gl_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
  72.          break;
  73.    }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /*
  80.  * Apply the alpha test to a span of pixels.
  81.  * In:  rgba - array of pixels
  82.  * In/Out:  mask - current pixel mask.  Pixels which fail the alpha test
  83.  *                 will set the corresponding mask flag to 0.
  84.  * Return:  0 = all pixels in the span failed the alpha test.
  85.  *          1 = one or more pixels passed the alpha test.
  86.  */
  87. GLint gl_alpha_test( const GLcontext* ctx,
  88.                      GLuint n, CONST GLubyte rgba[][4], GLubyte mask[] )
  89. {
  90.    GLuint i;
  91.    GLubyte ref = ctx->Color.AlphaRef;
  92.  
  93.    /* switch cases ordered from most frequent to less frequent */
  94.    switch (ctx->Color.AlphaFunc) {
  95.       case GL_LESS:
  96.          for (i=0;i<n;i++) {
  97.         mask[i] &= (rgba[i][ACOMP] < ref);
  98.      }
  99.      return 1;
  100.       case GL_LEQUAL:
  101.          for (i=0;i<n;i++) {
  102.         mask[i] &= (rgba[i][ACOMP] <= ref);
  103.      }
  104.      return 1;
  105.       case GL_GEQUAL:
  106.          for (i=0;i<n;i++) {
  107.         mask[i] &= (rgba[i][ACOMP] >= ref);
  108.      }
  109.      return 1;
  110.       case GL_GREATER:
  111.          for (i=0;i<n;i++) {
  112.         mask[i] &= (rgba[i][ACOMP] > ref);
  113.      }
  114.      return 1;
  115.       case GL_NOTEQUAL:
  116.          for (i=0;i<n;i++) {
  117.         mask[i] &= (rgba[i][ACOMP] != ref);
  118.      }
  119.      return 1;
  120.       case GL_EQUAL:
  121.          for (i=0;i<n;i++) {
  122.         mask[i] &= (rgba[i][ACOMP] == ref);
  123.      }
  124.      return 1;
  125.       case GL_ALWAYS:
  126.      /* do nothing */
  127.      return 1;
  128.       case GL_NEVER:
  129.          /* caller should check for zero! */
  130.      return 0;
  131.       default:
  132.      gl_problem( ctx, "Invalid alpha test in gl_alpha_test" );
  133.          return 0;
  134.    }
  135.    /* Never get here */
  136.    /*return 1;*/
  137. }
  138.